//
//  ViewController.m
//  CoreDataAddress
//
//  Created by dllo on 15/11/21.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ViewController.h"
#import "ZDQAddAddressViewController.h"
#import "ZDQAddressTableViewCell.h"
#import "ZDQMoreAddressViewController.h"
#import "ZDQSearchViewController.h"
#import "ZDQCoreDataManager.h"
#import "AddressBook.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) NSArray *array;
@property(nonatomic, retain) ZDQCoreDataManager *manamager;


@end

@implementation ViewController
- (void)dealloc {
    
    [_tableView release];
    [_array release];
    [_manamager retain];
    [super dealloc];
    
}


- (void)viewWillAppear:(BOOL)animated {
    
    NSFetchRequest *requset = [[NSFetchRequest alloc] initWithEntityName:@"AddressBook"];
  
    
        NSError *error;
    
    self.array = [self.manamager.managedObjectContext executeFetchRequest:requset error:&error];
    
    NSLog(@"%ld", self.array.count);
    NSLog(@"%@", self.array);

    [self.tableView reloadData];
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.rowHeight = 150;
    [_tableView release];
    
    //设置代理人
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(buttonAction:)] autorelease];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(20, 20, 200, 30);
    button.layer.borderWidth = 1;
    button.layer.cornerRadius = 15;
    [button setTitle:@"搜索名字" forState:UIControlStateNormal];
    button.tintColor = [UIColor lightGrayColor];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = button;
    
    self.manamager = [ZDQCoreDataManager defaults];
    NSLog(@"url ========  %@", [self.manamager applicationDocumentsDirectory]);
    
//    NSFetchRequest *requset = [[NSFetchRequest alloc] initWithEntityName:@"AddressBook"];
//    
//    NSLog(@"%@", requset);
//    NSError *error;
//    
//    self.array = [self.manamager.managedObjectContext executeFetchRequest:requset error:&error];
//    
//    NSLog(@"%ld", self.array.count);
//    NSLog(@"%@", self.array);

    
}




#pragma mark -- dataSource 和 delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.array.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reuse = @"reuse";
    ZDQAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    
    if (!cell) {
        cell = [[[ZDQAddressTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
        
    }
    
    AddressBook *Add = self.array[indexPath.row];
    
    NSString *str = [NSString stringWithFormat:@"姓名:  %@", Add.name];
    cell.nameLabel.text = str;
    NSString *Str = [NSString stringWithFormat:@"性别:  %@", Add.sex];
    cell.sexLabel.text = Str;
    NSString *string = [NSString stringWithFormat:@"学号:  %@", Add.number];
    cell.numberLabel.text = string;
    
    return cell;
    
    
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZDQMoreAddressViewController *moreVC = [[ZDQMoreAddressViewController alloc] init];
    [self.navigationController pushViewController:moreVC animated:YES];
    [moreVC release];
    
    AddressBook *Add = self.array[indexPath.row];
    moreVC.Add = Add;
    
}



#pragma mark 创建button的点击方法
- (void)buttonAction:(UIBarButtonItem *)button {
    
    ZDQAddAddressViewController *zdqAddVC = [[ZDQAddAddressViewController alloc] init];
    [self.navigationController pushViewController:zdqAddVC animated:YES];
    [zdqAddVC release];
    
    
}




- (void)buttonClick:(UIButton *)button {
    
    ZDQSearchViewController *searchVC = [[ZDQSearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
    [searchVC release];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
